/**
 * Copyright (c) 2020 kedacom
 * OpenATC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 **/
<template>
  <div class="serviceroute-plancontent">
    <PlanChart
      ref="planchart"
      :route="route"
      :tabName="activeName"
      @onPreviewResearch="onPreviewResearch"
    />
    <SchemeConfiguration
      ref="config"
      @research="research"
    />
    <select-control v-show="false" ref="selectControl"></select-control>
    <PlanExecute ref="planExecute"
        @research="research"></PlanExecute>
  </div>
</template>

<script>
/*eslint-disable*/
// @ts-ignore
import SchemeConfiguration from './components/schemeConfiguration'
// @ts-ignore
import PatternTable from './pattern/patternTable'
// @ts-ignore
import PlanChart from './pattern/planChart/routePreview'
// @ts-ignore
import PlanExecute from './components/planExecute'
import {
  GetSingleViproute,
  GetViprouteStatus
} from '../../api/service'
import { GetDeviceByIds } from '../../api/device'
// @ts-ignore
import SelectControl from '../../views/Service/components/SelectControl'
import { getMessageByCode } from '../../utils/responseMessage'
export default {
  name: 'PlanContent',
  components: {
    PlanExecute,
    SchemeConfiguration,
    PatternTable,
    PlanChart,
    SelectControl
  },
  props: {
    chooseId: {
      type: Number
    }
  },
  data () {
    return {
      timer: 0,
      activeName: 'second',
      messageboxVisible: false, // 关闭界面二期确认弹窗是否显示
      addNum: 0,
      routeData: {}, // 单个协调路线的全部信息
      patternList: [],
      phaseList: [],
      greenwave: [],
      node: {},
      route: {},
      stateList: [],
      devicesData: [],
      deviceIds: []
    }
  },
  watch: {
    chooseId (val) {
      this.setRoute()
    }
  },
  beforeDestroy () {
    clearInterval(this.timer)
  },
  methods: {
    async resetInterval() {
      if (this.timer) {
        // @ts-ignore
        clearInterval(this.timer)
        this.timer = 0
      }
      // @ts-ignore
      this.timer = setInterval(async() => {
        this.setRoute()
      }, 30 * 1000)
    },
    getControlName (control) {
      let res
      // @ts-ignore
      res = this.$refs.selectControl.getNameById(control)
      return res
    },
    getSingleViproute (routeId) {
      let _this = this
      // @ts-ignore
      return new Promise((resolve, reject) => {
        // @ts-ignore
        GetSingleViproute(routeId).then(res => {
          if (!res.data.success) {
            _this.$message.error(getMessageByCode(res.data.code, _this.$i18n.locale))
            return
          }
          const route = res.data.data
          resolve(route)
        })
      })
    },
    getViprouteStatus () {
      let _this = this
      // @ts-ignore
      return new Promise((resolve, reject) => {
        // @ts-ignore
        GetViprouteStatus(_this.chooseId).then(res => {
          if (!res.data.success) {
            _this.$message.error(getMessageByCode(res.data.code, _this.$i18n.locale))
            // @ts-ignore
            resolve()
          }
          let stateList = res.data.data
          _this.stateList = stateList
          resolve(stateList)
        })
      })
    },
    getDeviceByIds () {
      // 获取设备表格信息
      this.devicesData = []
      let _this = this
      // @ts-ignore
      return new Promise((resolve, reject) => {
        _this.deviceIds = _this.routeData.devs.map(ele => ele.agentid)
        // @ts-ignore
        GetDeviceByIds(_this.deviceIds).then(res => {
          if (!res.data.success) {
            _this.$message.error(getMessageByCode(res.data.code, _this.$i18n.locale))
            // @ts-ignore
            resolve()
          }
          _this.devicesData = res.data.data
          resolve(_this.devicesData)
        })
      })
    },
    async setRoute () {
      if (!this.chooseId || this.chooseId === 0) {
        // reset
        this.routeData = {}
        this.devicesData = []
        this.route = {}
        return false
      }
      const route = await this.getSingleViproute(this.chooseId)
      this.routeData = route
      if (this.routeData && this.routeData.devs.length === 0) {
        // reset
        this.devicesData = []
      }
      for (let item of this.routeData.devs) {
        item.currentControlName = this.getControlName(item.currentControl)
        let controlName = this.getControlName(item.control)
        item.controlName = controlName
        if (!item.terminalname && item.terminal > 0) {
          item.terminalname = this.$t('openatc.greenwaveoptimize.pattern') + item.terminal
        }
      }
      if (!this.routeData || this.routeData.devs.length === 0) {
      }
      this.route = this.routeData
      // this.$emit('research', this.route)
    },
    // @ts-ignore
    handleClick (tab, event) {
      this.activeName = tab.name
    },
    checkEdit () {
      let res = true
      for (let item of this.route.devs) {
        if (item.state === 1) {
          res = false
          this.$message.warning(this.$t('openatc.dutyroute.notAllowedToEdit'))
          break
        }
      }
      return res
    },
    checkConfig () {
      let res = true
      if (this.route.devs && this.route.devs.length === 0) {
        res = false
        this.$message.warning(this.$t('openatc.dutyroute.noIntersection'))
      }
      return res
    },
    handleAdd () {
      const config = this.$refs.config
      if (config) {
        // @ts-ignore
        config.onEditClick({})
      }
    },
    handleEdit () {
      const config = this.$refs.config
      // @ts-ignore
      config && config.onEditClick(this.routeData)
    },
    handleBatchExec () {
      if (!this.checkConfig()) {
        return false
      }
      const routeData = this.routeData
      let planContent = this.$refs.planExecute
      // @ts-ignore
      planContent.onExecuteClick(this.route.id, routeData)
    },
    onPreviewResearch (val) {
      this.$emit('onExecuteStateChange', val)
      if (val) {
        this.setRoute()
      } else {
        this.resetInterval()
      }
    },
    research (rotue) {
      if (rotue) {
        // @ts-ignore
        this.chooseId = rotue.id
      }
      this.$emit('research', rotue)
      this.setRoute()
    }
  }
}
</script>
